{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7f74ee52",
   "metadata": {},
   "source": [
    "## 处理有序数据  \n",
    "* 列表\n",
    "> 1.有序的可变对象集合  \n",
    "\n",
    "* 字典  \n",
    "> 1.无序的键/值对集合（字典存储键/值对）\n",
    "\n",
    "* 元组  \n",
    "> 1.有序的不可变对象集合  \n",
    "\n",
    "* 集合  \n",
    "> 1.无序的唯一对象集合（不允许有重复对象）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d075b888",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 数字\n",
    "import random\n",
    "wait_time = random.randint(1,60)\n",
    "wait_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5ba787b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##字符串\n",
    "word = \"bottles\"\n",
    "word\n",
    "len(word)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a073c800",
   "metadata": {},
   "source": [
    "#### in检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "70e9c8ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "##列表 列表总是用中括号包围，用逗号隔开\n",
    "odds = [1,3,5,7,9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "baa47dbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = ['q', 'a', 'e', 'i', 'o', 'u']\n",
    "word = \"milliways\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cf1bb399",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "for letter in word:      ##取单词中的各个字母\n",
    "    if letter in vowels: ##如果它在“vowels”\n",
    "        print(letter)     ## 在屏幕中现实这个字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "42f57412",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = []\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cf8f52f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.append('a')  ##使用append将对象增加到现有列表\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "54bf1037",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.append('e')   ##增加更多对象\n",
    "found.append('o')\n",
    "found.append('i')\n",
    "len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d68c676a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'o', 'i']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5ebcf2fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'o', 'i', 'u']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 用in检查成员关系\n",
    "if 'u' not in found:\n",
    "    found.append('u')\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "49f5d4c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['q', 'a', 'e', 'i', 'o', 'u']\n",
    "word = \"milliways\"\n",
    "found = [ ]\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f13ee4ed",
   "metadata": {},
   "source": [
    "### 从列表删除对象\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b7525173",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,2,3,4,5]\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ff6452ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums.remove(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "6279a3b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 4, 5]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bdca050",
   "metadata": {},
   "source": [
    "### 从列表弹出对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d4ddab70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.pop()  ##没有告诉要删除哪个对象，默认弹出列表中的最后一项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "bf331193",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 4]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a24a376",
   "metadata": {},
   "source": [
    "### 从列表扩展对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e5ebba49",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums.extend([2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "e7acc6c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 4, 2]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dcb00df",
   "metadata": {},
   "source": [
    "### 从列表插入对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0ef65db4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 4, 2]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "4da0625d",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums.insert(0,1)  ##0表示插入对象的索引在这个位置之前 1表示要插入的值（对象）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "7ae27671",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 3, 4, 2]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "2cf0e61f",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums.insert(1,\"two\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9267aea1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'two', 1, 3, 4, 2]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dd7512b",
   "metadata": {},
   "source": [
    "### 中括号的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "e701929b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "4de1468e",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "pop from empty list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_10824/1044205242.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mplist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mplist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mplist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"'\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mplist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mplist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mplist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mo\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mIndexError\u001b[0m: pop from empty list"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    plist.pop(i)\n",
    "plist.pop(i)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(i),plist.pop(o)])\n",
    "plist.insert(2,plist.pop(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "2d4dadf7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "new_phrase =''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "bc93e292",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "7ee990e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second = first\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "8185100c",
   "metadata": {},
   "outputs": [],
   "source": [
    "second.append(6)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "ef684a1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "06903386",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first    ##second和first指向同一个数据，如果修改一下另一个也会改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "f093c6b0",
   "metadata": {},
   "outputs": [],
   "source": [
    " saying = \"Don't panic!\"\n",
    "letters = list(saying)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "7b4fda06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "b6219754",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D'"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "ef66fc5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'i'"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[-3]   ##负索引值从右往左数 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b977fc66",
   "metadata": {},
   "source": [
    "### 开始 结束 步长值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d27269d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "letters[start: stop: step]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7eecc0f",
   "metadata": {},
   "source": [
    "### 切片的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "e6e43b9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "cfed2cf7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', \"'\", 'p', 'i']"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[0:10:3]    ##每3个字母选择1个，直到（但不包括）索引位置10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8f43cc87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[3:]    ##从第三个开始，然后给出其余所以字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "0a9bd6fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'n', 't', 'p', 'n', 'c']"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[: :2]   ##每2个字母选择1个"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72615364",
   "metadata": {},
   "source": [
    "## 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "b85c47ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "phrase =\"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "42c01157",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2, plist.pop(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "194752d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adfad902",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
